# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf8')
import scrapy
from scrapy.contrib.spiders import CrawlSpider, Rule
from zhuaqu.items import DmozItem
from scrapy.selector import HtmlXPathSelector
from scrapy.contrib.linkextractors import LinkExtractor

#****************************************
#各种初始变量设置
#当前抓取的index
__INDEX__ = 0

#成功抓取数量
__SUCCESS__ = 0

class MySpider(CrawlSpider):
    name = 'yaoZhi'
    allowed_domains = ['yaozh.com']
    start_urls = ['http://db.yaozh.com/pijian/1.html']

    rules = (
        # 提取匹配 'category.php' (但不匹配 'subsection.php') 的链接并跟进链接(没有callback意味着follow默认为True)
        Rule(LinkExtractor(allow=(r"http://about.yaozh.com/about")), follow=True,callback='parse_instruct'),
        # Rule(LinkExtractor(allow=("index\.php\?action=yibao")), follow=True, callback='parse_yiBao')

        # 提取匹配 'item.php' 的链接并使用spider的parse_item方法进行分析
        # Rule(LinkExtractor(allow=('item\.php', )), callback='parse_item'),
    )

    # def parse_start_url(self, response):
    #     print (2132132132)
    #     self.log('Hi, this is an item page12312313! %s' % response.url)
    #     item = DmozItem()
    #     item['title'] = response.xpath('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
    #     item['yaoZhiID'] = response.xpath('//td[@id="item_name"]/text()').extract()
    #     item['intro'] = response.xpath('//td[@id="item_description"]/text()').extract()
    #     return item

    def parse_instruct(self, response):
        print (5465465465)
        self.log('Hi, this is an item page5646464564! %s' % response.url)
        item = DmozItem()
        item['title'] = response.xpath('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
        item['yaoZhiID'] = response.xpath('//td[@id="item_name"]/text()').extract()
        item['intro'] = response.xpath('//td[@id="item_description"]/text()').extract()
        return item

    def parse_yiBao(self, response):
        print (78787878)
        self.log('Hi, this is an item page97897979! %s' % response.url)
        item = DmozItem()
        item['title'] = response.xpath('//td[@id="item_id"]/text()').re(r'ID: (\d+)')
        item['yaoZhiID'] = response.xpath('//td[@id="item_name"]/text()').extract()
        item['intro'] = response.xpath('//td[@id="item_description"]/text()').extract()
        return item